package com.wskeee.ec.containers
{
	import flash.display.DisplayObject;
	import flash.events.Event;

	public class HBox extends Box
	{
		public function HBox()
		{
			super();
			_direction = HORIZONTAL;
		}
		
		//========================= validate ====================
		override protected function measure():void
		{
			super.measure();
			measureWidth = _paddingLeft + _paddingRight + Math.max(numChildren-1,0)*horizontalGap + contentWidth;
			measureHeight = _paddingTop + _paddingBottom + maxItemHeight;
		}
		override protected function updateDisplayList():void
		{
			super.updateDisplayList();
			
			var maxWidth:Number = !isNaN(width) ? width : measureWidth;
			var maxHeight:Number = !isNaN(height) ? height : measureHeight;
			
			var ypos:Number = _paddingTop;
			var xpos:Number;
			var child:DisplayObject;
			
			borderDrawer.lineStyle(0.1,borderColor as uint,0.5);
			
			switch(horizontalAlign)
			{
				case LayoutConst.LEFT:
					xpos = _paddingLeft;
					break;	
				case LayoutConst.CENTER:
					xpos = (maxWidth - measureWidth)/2;
					break;	
				case LayoutConst.RIGHT:
					xpos = maxWidth - measureWidth;
					break;	
				
			}
			for(var i:int = 0,len:int=numChildren; i<len; i++)
			{
				child = getChildAt(i);
				switch(verticalAlign)
				{
					case LayoutConst.TOP:
						child.y = ypos;
						break;	
					case LayoutConst.MIDDLE:
						child.y = (maxHeight - child.height)/2;
						break;
					case LayoutConst.BOTTOM:
						child.y = maxHeight - child.height - _paddingBottom;
						break;
				}
				child.x = xpos;
				xpos += child.width + _horizontalGap;
				
				if(border)
					borderDrawer.drawRect(child.x,_paddingTop,child.width,maxHeight - _paddingTop - paddingBottom);
			}
			if(border)
				borderDrawer.drawRect(0,0,maxWidth,maxHeight);
			
			if(isNaN(width) || isNaN(height))
				setActualSize(maxWidth,maxHeight,false);
			
			if(oldMeasureWidth!=measureWidth || oldMeasureHeight!=measureHeight)
				dispatchEvent(new Event(Event.RESIZE));
		}
		//========================= end =========================
	}
}